package com.graduation.smartparkingplatform.Controller;

import com.github.pagehelper.PageInfo;
import com.graduation.smartparkingplatform.Pojo.Private;
import com.graduation.smartparkingplatform.Pojo.Space;
import com.graduation.smartparkingplatform.Response.Response;
import com.graduation.smartparkingplatform.Response.ResponseUtils;
import com.graduation.smartparkingplatform.Service.PrivateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/private")
public class PrivateController {
  @Autowired PrivateService privateService;

  // 获取私有车位列表
  @GetMapping("/privateList")
  public Response getPrivateList(
      @RequestParam(value = "page", defaultValue = "1") Integer page,
      @RequestParam(value = "count", defaultValue = "10") Integer count) {
    try {
      PageInfo<Private> privateList = privateService.getPrivateList(page, count);
      return ResponseUtils.buildSuccess(privateList);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }

  // 获取私有人下的车位列表
  @GetMapping("/privateLists")
  public Response getPrivateLists(@RequestParam("privateId") Integer privateId) {
    try {
      List<Space> privateLists = privateService.getPrivateLists(privateId);
      return ResponseUtils.buildSuccess(privateLists);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }

  // 模糊搜索
  @GetMapping("/searchPrivate")
  public Response searchPrivate(
      @RequestParam(value = "page", defaultValue = "1") Integer page,
      @RequestParam(value = "count", defaultValue = "10") Integer count,
      @RequestParam(value = "privateName", defaultValue = "null") String privateName) {
    try {
      PageInfo<Private> searchList = privateService.searchPrivate(page, count, privateName);
      return ResponseUtils.buildSuccess(searchList);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }

  // 增加私有信息
  @PostMapping("/addPrivate")
  public Response addPrivate(@RequestBody Private privatee) {
    Boolean addPrivate = privateService.addPrivate(privatee);
    if (addPrivate) {
      return ResponseUtils.buildSuccess("增加私有车位信息成功");
    }
    return ResponseUtils.buildError("增加私有车位信息失败");
  }

  // 修改私有车位
  @PostMapping("/changeInfo")
  public Response changeInfo(@RequestBody Private privatee) {
    Boolean changeInfo = privateService.changeInfo(privatee);
    if (changeInfo) {
      return ResponseUtils.buildSuccess("修改私有车位信息成功");
    }
    return ResponseUtils.buildError("修改私有车位信息失败");
  }

  // 修改私有车位状态
  @PostMapping("/changeStatus")
  public Response changePrivateStatus(
      @RequestParam("privateId") Integer privateId, @RequestParam("status") Boolean status) {
    Boolean changeStatus = privateService.changePrivateStatus(privateId, status);
    if (changeStatus) {
      return ResponseUtils.buildSuccess("修改私有车位状态成功");
    }
    return ResponseUtils.buildError("修改私有车位状态失败");
  }

  // 删除私有车位
  @PostMapping("/deletePrivate")
  public Response deletePrivate(@RequestParam("privateId") Integer privateId) {
    Boolean delete = privateService.deletePrivate(privateId);
    if (delete) {
      return ResponseUtils.buildSuccess("删除私有车位信息成功");
    }
    return ResponseUtils.buildError("删除私有车位信息失败");
  }

  // 绑定停车位
  @PostMapping("/bindingSpace")
  public Response bindingSpace(
      @RequestParam("privateId") Integer privateId, @RequestParam("spaceId") Integer spaceId) {
    Boolean delete = privateService.bindingSpace(privateId, spaceId);
    if (delete) {
      return ResponseUtils.buildSuccess("绑定停车位成功");
    }
    return ResponseUtils.buildBUSSINESS_ERROR("绑定停车位失败");
  }
}
